package com.shanzmoo.job.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.job.base.BaseTask;
import com.shanzmoo.base.constant.MallOrderStatus;
import com.shanzmoo.db.mall.entity.MallOrderDetailEntity;
import com.shanzmoo.db.mall.entity.MallOrderEntity;
import com.shanzmoo.db.mall.service.IMallOrderDetailService;
import com.shanzmoo.db.mall.service.IMallOrderService;
import com.shanzmoo.db.order.entity.dto.MallOrderDeatilRedisDTO;
import com.shanzmoo.db.order.entity.dto.MallOrderRedisDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * 商城订单增量量缓存刷新
 *
 * @author： Gzhao 2021/1/17
 */
@Slf4j
@Component(value = "mallOrderRedisUpdateByAddTask")
public class MallOrderRedisUpdateByAddTask implements BaseTask {

    /**
     * 订单信息列表 key
     */
    private static final String allOrderListKey = "szm:mo:order:all_list";
    /**
     * 用户订单信息列表 key
     */
    private static final String memberOrderListKey = "szm:mo:order:member_list";
    /**
     * 用户订单明细列表 key
     */
    private static final String orderDetailListKey = "szm:mo:order:deatil_list";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IMallOrderService mallOrderService;

    @Autowired
    private IMallOrderDetailService mallOrderDetailService;

    @Override
    public String doJob() {
        String startTime = LocalDateTimeUtil.formatNormal(
                LocalDateTimeUtil.offset(LocalDateTime.now(), -20, ChronoUnit.MINUTES)
        );
        List<MallOrderEntity> mallOrderList = mallOrderService.list(
                new QueryWrapper<MallOrderEntity>()
                        .eq("deleted", false)
                        .ge("update_time", startTime)
                        .ne("status", MallOrderStatus.CLOSED)
        );

        if (CollectionUtil.isNotEmpty(mallOrderList)){

            for (MallOrderEntity orderEntity : mallOrderList){
                // 1.订单信息
                MallOrderRedisDTO redisDTO = JSON.parseObject(JSON.toJSONString(orderEntity), MallOrderRedisDTO.class);
                stringRedisTemplate.opsForHash()
                        .put(allOrderListKey, String.valueOf(orderEntity.getId()), JSON.toJSONString(redisDTO));

                // 2.用户订单
                String memberOrderListStr = (String)stringRedisTemplate.opsForHash()
                        .get(memberOrderListKey, String.valueOf(orderEntity.getMemberId()));

                List<Integer> memberOrderIds = StrUtil.isNotBlank(memberOrderListStr) ? JSON.parseArray(memberOrderListStr, Integer.class)
                        : new ArrayList<>();

                if (!memberOrderIds.contains(orderEntity.getId())) {
                    memberOrderIds.add(orderEntity.getId());
                }
                stringRedisTemplate.opsForHash()
                        .put(memberOrderListKey, String.valueOf(orderEntity.getMemberId()), JSON.toJSONString(memberOrderIds));

                // 3.订单详情
                List<MallOrderDetailEntity> orderDetailList = mallOrderDetailService.list(
                        new QueryWrapper<MallOrderDetailEntity>()
                                .eq("deleted", false)
                                .eq("order_id", orderEntity.getId())
                );

                List<MallOrderDeatilRedisDTO> orderDetailRedisList = JSON.parseArray(JSON.toJSONString(orderDetailList), MallOrderDeatilRedisDTO.class);
                stringRedisTemplate.opsForHash().put(orderDetailListKey, String.valueOf(orderEntity.getId()),
                        JSON.toJSONString(orderDetailRedisList));
            }
        }

        return "success";

    }
}
